#include "./app_task.h"
#include "../driver/cmd_uart.h"
#include "../xlib/compat.h"
#include "../service/conn.h"
#include "./handler_task.h"
#include "../utils/log.h"
#include "../cfg/app_info.h"
#include "../driver/door.h"
#include "../driver/sensor.h"
#include "../driver/nozzle.h"
#include "../driver/ctrl.h"
#include "./status_task.h"
#include "../driver/scan_uart.h"
#include "./draw_task.h"
#include "./water_heat_task.h"
#include "./steam_heat_task.h"
#include "./drain_task.h"

namespace task {

AppTask appTask;

using namespace driver;
using namespace cfg;

static void mcu_af_config() {
    RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;
    AFIO->MAPR = (0x01 << 4) + // USART3
        (0x02 << 24); // SWD
}

void AppTask::run() {
    mcu_af_config();
    cmdUart.init();
    appInfo.load();
    door.init();
    sensor.init();
    nozzle.init();
    ctrl.init();
    scanUart.init();

    println("app run");

    statusTask.start();
    drainTask.start();
    steamHeatTask.init_start();
    waterHeatTask.init_start();
    drawTask.init_start();
    handlerTask.init_start();
    service::conn_loop();
}

} // namespace task
